#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

class Solution {
public:
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        /* 建立优先队列 */
        /* 保留最小的k个数，大顶堆(同greater)， */
        auto cmp = [](pair<int, int> a, pair<int, int> b) -> bool {
            if (a.first > b.first) {
                return false;
            } else if (a.first < b.first) {
                return true;
            } else {
                return a.second < b.second;
            }
        };
        priority_queue<pair<int, int>, std::vector<pair<int, int>>, decltype(cmp)> priQue(cmp);

        for (int row = 0; row < mat.size(); row++) {
            int tmp = 0;
            for (int col = 0; col < mat[row].size(); col++) {
                tmp += mat[row][col];
            }

            auto pairVal = make_pair(tmp, row);
            priQue.push(pairVal);
            if (priQue.size() > k) {
                priQue.pop();
            }

        }

        vector<int> ret{};
        while (!priQue.empty()) {
            ret.push_back(priQue.top().second);
            priQue.pop();
        }

        reverse(ret.begin(), ret.end());
        return ret;
    }
};


int main()
{   
    Solution s;
    vector<vector<int>> vt = {{1,0,0,0},{1,1,1,1},{1,0,0,0},{1,0,0,0}};
    vector<int> ret = s.kWeakestRows(vt, 2);

    for (auto num : ret) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}